home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / MacPerl ƒ / Perl Source ƒ / Perl / macscripts / h2ph < prev    next >
Text File  |  1993-10-23  |  5KB  |  249 lines

  1. Perl -Sx "{0}"; Exit
  2. #!/usr/local/bin/perl
  3. 'di';
  4. 'ig00';
  5.  
  6. $perlincl = $ENV{'MPW'} . 'Libraries:PerlLibs:';
  7.  
  8. chdir $ENV{'CIncludes'} || die "Can't cd {CIncludes}";
  9.  
  10. @isatype = split(' ',<<END);
  11.     char    uchar    u_char
  12.     short    ushort    u_short
  13.     int    uint    u_int
  14.     long    ulong    u_long
  15.     FILE
  16. END
  17.  
  18. @isatype{@isatype} = (1) x @isatype;
  19.  
  20. @ARGV = ('-') unless @ARGV;
  21.  
  22. foreach $file (@ARGV) {
  23.     if ($file eq '-') {
  24.     open(IN, "-");
  25.     open(OUT, ">-");
  26.     }
  27.     else {
  28.     ($outfile = $file) =~ s/\.h$/.ph/ || next;
  29.     print "$file -> $outfile\n";
  30.     open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
  31.     open(OUT,">$perlincl$outfile") || die "Can't create $outfile: $!\n";
  32.     }
  33.     while (<IN>) {
  34.     chop;
  35.     while (/\\$/) {
  36.         chop;
  37.         $_ .= <IN>;
  38.         chop;
  39.     }
  40.     if (s:/\*:\200:g) {
  41.         s:\*/:\201:g;
  42.         s/\200[^\201]*\201//g;    # delete single line comments
  43.         if (s/\200.*//) {        # begin multi-line comment?
  44.         $_ .= '/*';
  45.         $_ .= <IN>;
  46.         redo;
  47.         }
  48.     }
  49.     if (s/^#\s*//) {
  50.         if (s/^define\s+(\w+)//) {
  51.         $name = $1;
  52.         $new = '';
  53.         s/\s+$//;
  54.         if (s/^\(([\w,\s]*)\)//) {
  55.             $args = $1;
  56.             if ($args ne '') {
  57.             foreach $arg (split(/,\s*/,$args)) {
  58.                 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
  59.                 $curargs{$arg} = 1;
  60.             }
  61.             $args =~ s/\b(\w)/\$$1/g;
  62.             $args = "local($args) = \@_;\n$t    ";
  63.             }
  64.             s/^\s+//;
  65.             do expr();
  66.             $new =~ s/(["\\])/\\$1/g;
  67.             if ($t ne '') {
  68.             $new =~ s/(['\\])/\\$1/g;
  69.             print OUT $t,
  70.               "eval 'sub $name {\n$t    ${args}eval \"$new\";\n$t}';\n";
  71.             }
  72.             else {
  73.             print OUT "sub $name {\n    ${args}eval \"$new\";\n}\n";
  74.             }
  75.             %curargs = ();
  76.         }
  77.         else {
  78.             s/^\s+//;
  79.             do expr();
  80.             $new = 1 if $new eq '';
  81.             if ($t ne '') {
  82.             $new =~ s/(['\\])/\\$1/g;
  83.             print OUT $t,"eval 'sub $name {",$new,";}';\n";
  84.             }
  85.             else {
  86.             print OUT $t,"sub $name {",$new,";}\n";
  87.             }
  88.         }
  89.         }
  90.         elsif (/^include\s+<(.*)>/) {
  91.         ($incl = $1) =~ s/\.h$/.ph/;
  92.         print OUT $t,"require '$incl';\n";
  93.         }
  94.         elsif (/^ifdef\s+(\w+)/) {
  95.         print OUT $t,"if (defined &$1) {\n";
  96.         $tab += 4;
  97.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  98.         }
  99.         elsif (/^ifndef\s+(\w+)/) {
  100.         print OUT $t,"if (!defined &$1) {\n";
  101.         $tab += 4;
  102.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  103.         }
  104.         elsif (s/^if\s+//) {
  105.         $new = '';
  106.         do expr();
  107.         print OUT $t,"if ($new) {\n";
  108.         $tab += 4;
  109.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  110.         }
  111.         elsif (s/^elif\s+//) {
  112.         $new = '';
  113.         do expr();
  114.         $tab -= 4;
  115.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  116.         print OUT $t,"}\n${t}elsif ($new) {\n";
  117.         $tab += 4;
  118.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  119.         }
  120.         elsif (/^else/) {
  121.         $tab -= 4;
  122.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  123.         print OUT $t,"}\n${t}else {\n";
  124.         $tab += 4;
  125.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  126.         }
  127.         elsif (/^endif/) {
  128.         $tab -= 4;
  129.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  130.         print OUT $t,"}\n";
  131.         }
  132.     }
  133.     }
  134.     print OUT "1;\n";
  135. }
  136.  
  137. sub expr {
  138.     while ($_ ne '') {
  139.     s/^(\s+)//        && do {$new .= ' '; next;};
  140.     s/^(0x[0-9a-fA-F]+)//    && do {$new .= $1; next;};
  141.     s/^(\d+)//        && do {$new .= $1; next;};
  142.     s/^("(\\"|[^"])*")//    && do {$new .= $1; next;};
  143.     s/^'((\\"|[^"])*)'//    && do {
  144.         if ($curargs{$1}) {
  145.         $new .= "ord('\$$1')";
  146.         }
  147.         else {
  148.         $new .= "ord('$1')";
  149.         }
  150.         next;
  151.     };
  152.     s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
  153.         $new .= '$sizeof';
  154.         next;
  155.     };
  156.     s/^([_a-zA-Z]\w*)//    && do {
  157.         $id = $1;
  158.         if ($id eq 'struct') {
  159.         s/^\s+(\w+)//;
  160.         $id .= ' ' . $1;
  161.         $isatype{$id} = 1;
  162.         }
  163.         elsif ($id eq 'unsigned') {
  164.         s/^\s+(\w+)//;
  165.         $id .= ' ' . $1;
  166.         $isatype{$id} = 1;
  167.         }
  168.         if ($curargs{$id}) {
  169.         $new .= '$' . $id;
  170.         }
  171.         elsif ($id eq 'defined') {
  172.         $new .= 'defined';
  173.         }
  174.         elsif (/^\(/) {
  175.         s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i;    # cheat
  176.         $new .= " &$id";
  177.         }
  178.         elsif ($isatype{$id}) {
  179.         if ($new =~ /{\s*$/) {
  180.             $new .= "'$id'";
  181.         }
  182.         elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
  183.             $new =~ s/\(\s*$//;
  184.             s/^[\s*]*\)//;
  185.         }
  186.         else {
  187.             $new .= $id;
  188.         }
  189.         }
  190.         else {
  191.         $new .= ' &' . $id;
  192.         }
  193.         next;
  194.     };
  195.     s/^(.)//            && do {$new .= $1; next;};
  196.     }
  197. }
  198. ##############################################################################
  199.  
  200.     # These next few lines are legal in both Perl and nroff.
  201.  
  202. .00;            # finish .ig
  203.  
  204. 'di            \" finish diversion--previous line must be blank
  205. .nr nl 0-1        \" fake up transition to first page again
  206. .nr % 0            \" start at page 1
  207. '; __END__ ############# From here on it's a standard manual page ############
  208. .TH H2PH 1 "August 8, 1990"
  209. .AT 3
  210. .SH NAME
  211. h2ph \- convert .h C header files to .ph Perl header files
  212. .SH SYNOPSIS
  213. .B h2ph [headerfiles]
  214. .SH DESCRIPTION
  215. .I h2ph
  216. converts any C header files specified to the corresponding Perl header file
  217. format.
  218. It is most easily run while in /usr/include:
  219. .nf
  220.  
  221.     cd /usr/include; h2ph * sys/*
  222.  
  223. .fi
  224. If run with no arguments, filters standard input to standard output.
  225. .SH ENVIRONMENT
  226. No environment variables are used.
  227. .SH FILES
  228. /usr/include/*.h
  229. .br
  230. /usr/include/sys/*.h
  231. .br
  232. etc.
  233. .SH AUTHOR
  234. Larry Wall
  235. .SH "SEE ALSO"
  236. perl(1)
  237. .SH DIAGNOSTICS
  238. The usual warnings if it can't read or write the files involved.
  239. .SH BUGS
  240. Doesn't construct the %sizeof array for you.
  241. .PP
  242. It doesn't handle all C constructs, but it does attempt to isolate
  243. definitions inside evals so that you can get at the definitions
  244. that it can translate.
  245. .PP
  246. It's only intended as a rough tool.
  247. You may need to dicker with the files produced.
  248. .ex
  249.